import Vue from 'vue'
import App from './App.vue'
import router from './router'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import 'font-awesome/css/font-awesome.css'
// import '@/assets/css/global.css'

import store from './store/index'

//全局引用请求插件
import {deleteRequest, getRequest, postRequest, putRequest} from "@/utils/api";
import {initMenu} from "@/utils/menus";

Vue.config.productionTip = false

Vue.use(ElementUI)

//插件形式使用请求
Vue.prototype.postRequest = postRequest
Vue.prototype.putRequest = putRequest
Vue.prototype.getRequest = getRequest
Vue.prototype.deleteRequest = deleteRequest
Vue.prototype.$store = store

// 使用 router.beforeEach 注册一个全局前置守卫

router.beforeEach((to, from, next) => {

    // to 要去的路由; from 来自哪里的路由 ; next() 放行

    // 用户登录成功时，把 token 存入 sessionStorage，如果携带 token，初始化菜单，放行

    if (window.sessionStorage.getItem('token')) {

        initMenu(router, store)


        next();

    }
    // 如果用户不存在
    // else if (!window.sessionStorage.getItem("user")) {
    //     return getRequest('/user/info').then(resp => {
    //         if (resp) {
    //             //存入用户信息
    //             window.sessionStorage.setItem("user", JSON.stringify(resp.data));
    //             next();
    //         }
    //     });
    //     next()
    // }
    else {

        if (to.path === '/') {

            next()

        } else {

            next('/?redirect=' + to.path)

        }

    }

})

new Vue({
    router,
    store,
    render: h => h(App)
}).$mount('#app')
